/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ke.me.muhia.mvn.app.main;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 *
 * @author Kenneth
 */
public class WorkerRunner extends QuartzJobBean {

    private static final Logger logger = LoggerFactory.getLogger(WorkerRunner.class);

    @Override
    protected void executeInternal(JobExecutionContext jec) throws JobExecutionException {
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
            ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) context.getBean("taskExecutor");
            taskExecutor.execute(new WorkerServer("ucipParams1"));

            for (;;) {
                int count = taskExecutor.getActiveCount();
                logger.info("Active Threads : " + count);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    logger.error("WorkerRunner.executeInternal", e);
                }
                if (count == 0) {
                    taskExecutor.shutdown();
                    break;
                }
            }

        } catch (Exception e) {
            logger.error("WorkerRunner.executeInternal", e);
        }
    }

}
